package com.sgb.member.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import com.google.gson.Gson;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberModifyBinding;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.view_model.MemberModifyViewModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;
import java.util.ArrayList;
import java.util.List;


/**
 * 会员修改
 */
public class MemberModifyActivity extends AppActivity {
    private ActivityMemberModifyBinding binding;
    private MemberModifyViewModel viewModel;
    private MemberBean bean;
    private List<CommonPopEntity> memberTypes;
    private String itemRelation;
    private String checkType="1";

    public static void start(Activity activity, Bundle bundle) {
        Intent intent = new Intent(activity, MemberModifyActivity.class);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void initView() {
        viewModel = new ViewModelProvider(this).get(MemberModifyViewModel.class);
        Bundle bundle = getIntent().getExtras();
        bean = (MemberBean) bundle.getSerializable("bean");
        checkType =bundle.getString("checkType");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_member_modify);
        binding.actionBar.ivBack.setOnClickListener(v -> finish());
        binding.actionBar.titleView.setText("会员修改");
        binding.setBean(bean);
        binding.setLifecycleOwner(this);
        receiverLiveData();
        memberTypes = new ArrayList<>();
        if (!TextUtils.isEmpty(bean.getFirstCategoryNo())) {
            String[] firsts = bean.getFirstCategoryNo().split(",");
            List<String> first_names = bean.getFirstCategoryName();
            if (firsts.length > 0 && first_names.size() > 0) {
                for (int i = 0; i < first_names.size(); i++) {
                    memberTypes.add(new CommonPopEntity(firsts[i], first_names.get(i)));
                }
            }
        }
        List<String> ls = bean.getAllCategoryName();
        if (ls != null) {
            if (ls.size() > 0) {
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < ls.size(); i++) {
                    if (i == 0) {
                        stringBuilder.append(ls.get(i));
                    } else {
                        stringBuilder.append(",");
                        stringBuilder.append(ls.get(i));
                    }
                }
                binding.itemType.setText(stringBuilder.toString());
            }
        }
        itemRelation = bean.getMemberUpdateLeverType();
        if (itemRelation.equals("1")) {
            binding.itemRelation.setText("供方会员");
        } else
            binding.itemRelation.setText("需方会员");
        binding.itemType.setOnClickListener(v -> {
            //	供需关系 relation 1.供方会员 2.需方会员
            if (itemRelation.equals("1")) {
                // ascription 0 供方 1需方
                ChooseMemberTypeActivity.start(MemberModifyActivity.this, memberTypes, "0", MemberConfig.REQ_MEMBER_TYPE);
            } else {
                ChooseMemberTypeActivity.start(MemberModifyActivity.this, memberTypes, "1", MemberConfig.REQ_MEMBER_TYPE);
            }
        });
        submitData();
    }

    private void submitData() {
        binding.tvSubmit.setOnClickListener(v -> {
            String contacts = binding.etContacts.getText().toString().trim();
            if (TextUtils.isEmpty(contacts)) {
                MToast.showToast(MemberModifyActivity.this, "联系人不能为空！");
                return;
            }
            String phone = binding.etPhone.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                MToast.showToast(MemberModifyActivity.this, "联系电话不能为空！");
                return;
            }
            if (!CheckUtils.isPhoneNumberValid(phone)) {
                MToast.showToast(getActivity(), "请填写正确的联系电话!");
                return;
            }
            if (memberTypes.size() == 0) {
                MToast.showToast(MemberModifyActivity.this, "请选择会员类型！");
                return;
            }

            viewModel.getMemberTypeList(memberTypes, contacts,
                    bean.getLevel(), phone, itemRelation,bean.getNo(),checkType);
        });
    }

    private void receiverLiveData() {
        viewModel.msgLiveData.observe(this, s -> MToast.showToast(MemberModifyActivity.this, s));
        viewModel.isLoadLiveData.observe(this, aBoolean -> {
            if (aBoolean) {
                DialogHelper.showProgressDialog(MemberModifyActivity.this, "正在上传");
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        viewModel.isSuccessLiveData.observe(this, aBoolean -> {
            if (aBoolean){
                MemberModifyActivity.this.finish();
            }
        });
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null) {
            LogUtil.d("TAG", "onActivityResult chooseFile data is null");
            return;
        }
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case MemberConfig.REQ_MEMBER_TYPE:
                    memberTypes = (List<CommonPopEntity>) intent.getSerializableExtra("MEMBER_DATA");
                    if (memberTypes != null && memberTypes.size() > 0) {
                        StringBuilder sb = new StringBuilder();
                        for (CommonPopEntity memberType : memberTypes) {
                            sb.append(memberType.getTypeName()).append(",");
                        }
                        String str = sb.substring(0, sb.toString().length() - 1);
                        binding.itemType.setText(str);
                    } else {
                        binding.itemType.setText("");
                    }
                    break;
            }
        }
    }


}
